﻿Option Explicit On
Option Strict On

Imports System.Threading
Imports Microsoft.SPOT
Imports Gadgeteer

'  Original Gadgeteer framework Copyright (c) Microsoft Corporation. (http://gadgeteer.codeplex.com/)
'  
'  Gadgeteer Light made by Stefan Thoolen (http://gadgeteerlight.codeplex.com/)
'
'  Licensed under the Apache License, Version 2.0 (the "License");
'  you may not use this file except in compliance with the License.
'  You may obtain a copy of the License at
'
'      http://www.apache.org/licenses/LICENSE-2.0
'
'  Unless required by applicable law or agreed to in writing, software
'  distributed under the License is distributed on an "AS IS" BASIS,
'  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
'  See the License for the specific language governing permissions and
'  limitations under the License.
Namespace GHI_Electronics_EBlock_Expansion

    Public Module Module1

        Dim Led As Gadgeteer.Interfaces.DigitalOutput
        Dim WithEvents Button1 As Gadgeteer.Interfaces.InterruptInput
        Dim WithEvents Button2 As Gadgeteer.Interfaces.InterruptInput
        Dim Potmeter As Gadgeteer.Interfaces.AnalogInput
        Dim Piezo As Gadgeteer.Interfaces.PWMOutput

        Sub Main()
            ' Use this demo to test D and O E-Block pins (requires a gadgeteer X or Y socket)
            LedButton(socket:=1, buttonPin:=3, ledPin:=4)

            ' Use this demo to test E and O E-Block pins (requires a gadgeteer A socket)
            'LedPotentiometer(socket:=2, potmeterPin:=5, ledPin:=4)

            ' Use this demo to test D and O E-Block pins (requires a gadgeteer P socket)
            'PiezoButton(socket:=3, buttonPin:=3, piezoPin:=9)
        End Sub

        ''' <summary>Led and button sample; when the button is pressed, the led will emit</summary>
        ''' <param name="socket">[X Y] Gadgeteer socket of the E-Block module</param>
        ''' <param name="buttonPin">(D) E-Block pin to which the button is connected</param>
        ''' <param name="ledPin">(O) E-Block pin to which the led is connected</param>
        Sub LedButton(socket As Integer, buttonPin As Integer, ledPin As Integer)
            Dim Exp As Gadgeteer.Modules.GHIElectronics.EBlockExpansion = New Gadgeteer.Modules.GHIElectronics.EBlockExpansion(socket)

            Led = Exp.SetupDigitalOutput(CType(ledPin, Gadgeteer.Socket.Pin), False)
            Button1 = Exp.SetupInterruptInput(CType(buttonPin, Gadgeteer.Socket.Pin), Interfaces.GlitchFilterMode.On, Interfaces.ResistorMode.Disabled, Interfaces.InterruptMode.RisingAndFallingEdge)

            Thread.Sleep(Timeout.Infinite)
        End Sub

        Sub LedButton_Interrupt(sender As Gadgeteer.Interfaces.InterruptInput, value As Boolean) Handles Button1.Interrupt
            Led.Write(Not value)
        End Sub

        ''' <summary>Led and potentiometer sample; when the button is pressed, the led will emit</summary>
        ''' <param name="socket">[A] Gadgeteer socket of the E-Block module</param>
        ''' <param name="potmeterPin">(A) E-Block pin to which the potentiometer is connected</param>
        ''' <param name="ledPin">(O) E-Block pin to which the led is connected</param>
        Sub LedPotentiometer(socket As Integer, potmeterPin As Integer, ledPin As Integer)
            Dim Exp As Gadgeteer.Modules.GHIElectronics.EBlockExpansion = New Gadgeteer.Modules.GHIElectronics.EBlockExpansion(socket)

            Led = Exp.SetupDigitalOutput(CType(ledPin, Gadgeteer.Socket.Pin), False)
            Potmeter = Exp.SetupAnalogInput(CType(potmeterPin, Gadgeteer.Socket.Pin))

            Do
                Led.Write(True)
                Thread.Sleep(CInt(Potmeter.ReadProportion() * 1000))
                Led.Write(False)
                Thread.Sleep(CInt(Potmeter.ReadProportion() * 1000))
            Loop
        End Sub

        ''' <summary>Button and piezo sample; when the button is pressed, the piezo will make sounds</summary>
        ''' <param name="socket">[P] Gadgeteer socket of the E-Block module</param>
        ''' <param name="buttonPin">(D) E-Block pin to which the button is connected</param>
        ''' <param name="piezoPin">(O) E-Block pin to which the piezo is connected</param>
        Sub PiezoButton(socket As Integer, buttonPin As Integer, piezoPin As Integer)
            Dim Exp As Gadgeteer.Modules.GHIElectronics.EBlockExpansion = New Gadgeteer.Modules.GHIElectronics.EBlockExpansion(socket)

            Piezo = Exp.SetupPWMOutput(CType(piezoPin, Gadgeteer.Socket.Pin))
            Button2 = Exp.SetupInterruptInput(CType(buttonPin, Gadgeteer.Socket.Pin), Interfaces.GlitchFilterMode.On, Interfaces.ResistorMode.Disabled, Interfaces.InterruptMode.RisingAndFallingEdge)

            Thread.Sleep(Timeout.Infinite)
        End Sub

        ' Tone ladder
        Dim PiezoNotes() As UInteger = {1915000, 1700000, 1519000, 1432000, 1275000, 1136000, 1014000, 956000, 851000, 758000}
        ' Current index on the tone ladder
        Dim PiezoIndex As Integer = 0

        Sub PiezoButton_Interrupt(sender As Gadgeteer.Interfaces.InterruptInput, value As Boolean) Handles Button2.Interrupt
            If Not value Then
                ' Fetches the current note
                Dim Note As UInteger = PiezoNotes(PiezoIndex)
                ' Configures the PWM port
                Piezo.SetPulse(CUInt(Note * 2), Note)
                ' Let it play for 0.5 sec
                Piezo.Active = True
                Thread.Sleep(500)
                Piezo.Active = False
                ' Decides which note should be next
                PiezoIndex = PiezoIndex + 1
                If PiezoIndex = PiezoNotes.Length Then PiezoIndex = 0
            End If
        End Sub

    End Module

End Namespace